﻿using Microsoft.VisualBasic;

namespace _238._除自身以外数组的乘积
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var result=Solution.ProductExceptSelf(new int[] { 1, 2, 3, 5 });
            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
        }
    }
    public class Solution
    {
        public static int[] ProductExceptSelf(int[] nums)
        {

            int n = nums.Length;
            int[] leftProduct = new int[n];
            int[] rightProduct = new int[n];
            int[] answer = new int[n];

            // 计算左侧所有元素的乘积
            leftProduct[0] = 1;
            for (int i = 1; i < n; i++)
            {
                leftProduct[i] = leftProduct[i - 1] * nums[i - 1];
            }

            // 计算右侧所有元素的乘积
            rightProduct[n - 1] = 1;
            for (int i = n - 2; i >= 0; i--)
            {
                rightProduct[i] = rightProduct[i + 1] * nums[i + 1];
            }

            // 计算答案
            for (int i = 0; i < n; i++)
            {
                answer[i] = leftProduct[i] * rightProduct[i];
            }

            return answer;
        }
    }
}
